package com.baidu.health.service;

import com.baidu.health.pojo.resultVo.articleVo;
import com.baidu.health.pojo.resultVo.resAttentionList;
import com.baidu.health.pojo.resultVo.resultVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.health.pojo.TbAttention;
import com.baidu.health.mapper.TbAttentionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TbAttentionServiceImpl extends ServiceImpl<TbAttentionMapper, TbAttention>{

    @Autowired
    TbAttentionMapper attentionMapper;

    public resultVo attentionWriter(TbAttention attention) {
        QueryWrapper<TbAttention> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(TbAttention::getWriterId,attention.getWriterId());
        wrapper.lambda().eq(TbAttention::getUserId,attention.getUserId());

        TbAttention tbAttention = attentionMapper.selectOne(wrapper);

        if ( tbAttention == null ){
            attentionMapper.insert(attention);
            return new resultVo(200,"关注成功",null);
        }else {
//            if ( tbAttention.getDelFlag() == 0 ){
//                tbAttention.setDelFlag(1);
                attentionMapper.deleteById(tbAttention.getAttentionId());
                tbAttention.setUpdateTime(new Date());
                attentionMapper.updateById(tbAttention);
                return new resultVo(200,"已取消关注",null);
//            }else {
//                tbAttention.setDelFlag(0);
//                tbAttention.setUpdateTime(new Date());
//                attentionMapper.updateById(tbAttention);
//                return new resultVo(200,"关注成功",null);
//            }
        }
    }

    public articleVo getAttentionList(Integer pageNum, Integer pageSize) {
        List<resAttentionList> list = attentionMapper.getAttentionList(pageNum,pageSize);
        int size = attentionMapper.selectList(null).size();
        return new articleVo(true,200,"查询成功",list,size);
    }
}




